//
//  SAlertView.m
//  SAlterView
//
//  Created by Samuel on 15/9/1.
//  Copyright (c) 2015年 Samuel. All rights reserved.
//

#import "SAlertView.h"
#import "AppDelegate.h"

#define kUIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


@interface SAlertView()


@property (strong,nonatomic)UIView *bgView;

@end

@implementation SAlertView
{
    UILabel *titleLabel;
    UILabel *messageLabel;
    UIButton *cancelButton;
    UIButton *confirmButton;
    
    NSMutableArray *buttons;
}
+ (void)showSAlterViewTitle:(NSString*)title Message:(NSString*)message;
{
    SAlertView *asl = [[SAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:@"确定" confirmButtonTitles:nil];
    [asl show];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[[UIColor blackColor]colorWithAlphaComponent:.2]];
        buttons = [NSMutableArray array];
    }
    return self;
}


- (void)setDefaultAlterView:(NSString *)titles
                 andMessage:(NSString*)messages
            andCancelButton:(NSString*)cancelButtonstitles
           andConfirmButton:(NSString*)confirmButtontiles
{
    titleLabel = (titleLabel)?titleLabel:[[UILabel alloc]init];
    messageLabel = (messageLabel)?messageLabel:[[UILabel alloc]init];
    cancelButton = (cancelButton)?cancelButton:[UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton = (confirmButton)?confirmButton:[UIButton buttonWithType:UIButtonTypeCustom];
    self.bgView = (self.bgView)?self.bgView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 160)];
    
    [self.bgView setCenter:self.center];
    [self. bgView setBackgroundColor:[UIColor whiteColor]];
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 5;
    
    titleLabel.text = titles;
    [titleLabel setFont:[UIFont systemFontOfSize:15]];
    [titleLabel setTextColor:[UIColor darkGrayColor]];
    [titleLabel sizeToFit];
    [titleLabel setCenter:CGPointMake(CGRectGetWidth(self.bgView.frame)/2, 15+CGRectGetHeight(titleLabel.frame)/2)];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(10, 30+CGRectGetHeight(titleLabel.frame), CGRectGetWidth(self.bgView.frame)-20, 0.5)];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    
    messageLabel.text = messages;
    [messageLabel setFont:[UIFont systemFontOfSize:16]];
    [messageLabel setTextColor:[UIColor lightGrayColor]];
    [messageLabel sizeToFit];
    [messageLabel setCenter:CGPointMake(titleLabel.center.x, 20+line.frame.origin.y+CGRectGetHeight(messageLabel.frame)/2)];
    
    [cancelButton setTitle:cancelButtonstitles forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [cancelButton setTitleColor:kUIColorFromRGB(0x829FF4) forState:UIControlStateNormal];
    if (confirmButtontiles) {
        [cancelButton setFrame:CGRectMake(20, messageLabel.frame.origin.y+CGRectGetHeight(messageLabel.frame)+20, 100, 30)];
    }
    else
    {
        [cancelButton setFrame:CGRectMake((CGRectGetWidth(self.bgView.frame)-100)/2, messageLabel.frame.origin.y+CGRectGetHeight(messageLabel.frame)+20, 100, 30)];
    }
    
    [cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchDown];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 3;
    cancelButton.layer.borderWidth = 1.5;
    cancelButton.layer.borderColor = kUIColorFromRGB(0x829FF4).CGColor;
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    
    if (confirmButtontiles) {
        [confirmButton setTitle:confirmButtontiles forState:UIControlStateNormal];
        [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [confirmButton setFrame:CGRectMake(130, messageLabel.frame.origin.y+CGRectGetHeight(messageLabel.frame)+30, 100, 30)];
        [confirmButton addTarget:self action:@selector(confirmButton:) forControlEvents:UIControlEventTouchDown];
        confirmButton.layer.masksToBounds = YES;
        confirmButton.layer.cornerRadius = 3;
        confirmButton.layer.borderWidth = 1.5;
        confirmButton.layer.borderColor = kUIColorFromRGB(0x79BA3B).CGColor;
        [confirmButton setBackgroundColor:kUIColorFromRGB(0x79BA3B)];
        [buttons addObject:confirmButton];
        [self.bgView  addSubview:confirmButton];
    }
    
    
    [self.bgView  addSubview:titleLabel];
    [self.bgView  addSubview:line];
    [self.bgView  addSubview:messageLabel];
    [self.bgView  addSubview:cancelButton];
    
    [self addSubview:self.bgView];
    
}
- (void)cancelButton:(UIButton*)sender
{
    if ([self.delegate respondsToSelector:@selector(sAlertView:clickedButtonAtIndex:)]) {
        [self.delegate sAlertView:self clickedButtonAtIndex:0];
    }
    [self dismiss];
    
}
- (void)confirmButton:(UIButton*)sender
{
    if ([self.delegate respondsToSelector:@selector(sAlertView:clickedButtonAtIndex:)]) {
        [self.delegate sAlertView:self clickedButtonAtIndex:[buttons indexOfObject:sender]+1];
    }
}
- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                     delegate:(id /*<UIAlertViewDelegate>*/)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
            confirmButtonTitles:(NSString *)confirmButtonTitles;
{
    self = [self initWithFrame:[UIScreen mainScreen].bounds];//
    self.delegate = delegate;
    [self setDefaultAlterView:title andMessage:message andCancelButton:cancelButtonTitle andConfirmButton:confirmButtonTitles];
    return self;
}
- (instancetype)initWithView:(UIView *)centerView
                    delegate:(id /*<UIAlertViewDelegate>*/)delegate
                cancelButton:(UIButton *)cancelButtons
                ortherButton:(UIButton *)ortherButton, ... NS_REQUIRES_NIL_TERMINATION;
{
    self = [self initWithFrame:[UIScreen mainScreen].bounds];//
    self.delegate = delegate;
    self.bgView = (self.bgView)?self.bgView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(centerView.frame), CGRectGetHeight(centerView.frame))];
    
    [self. bgView setBackgroundColor:[UIColor whiteColor]];
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 5;
    [centerView setCenter:self.bgView.center];
    [self.bgView addSubview:centerView];
    
    [self.bgView setCenter:self.center];
    [self addSubview:self.bgView];
    
    cancelButton = cancelButtons;
    [cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchDown];
    
    if (ortherButton) {
        [buttons addObject:ortherButton];
        [ortherButton addTarget:self action:@selector(confirmButton:) forControlEvents:UIControlEventTouchDown];

    }
    va_list arguments;
    va_start(arguments, ortherButton);
    UIButton *getOrtherButton;
    if(ortherButton)
    {
        while((getOrtherButton = va_arg(arguments,UIButton *)))
        {
            if (getOrtherButton) {
                [buttons addObject:getOrtherButton];
                [getOrtherButton addTarget:self action:@selector(confirmButton:) forControlEvents:UIControlEventTouchDown];
            }
        }
    }
    va_end(arguments);
    return self;
}
- (void)show;
{
    NSEnumerator *frontToBackWindows = [[[UIApplication sharedApplication]windows]reverseObjectEnumerator];
    
    for (UIWindow *window in frontToBackWindows)
    {
        if (window.windowLevel == UIWindowLevelNormal) {
            [self setCenter:window.center];
            self.alpha = 0;
            [window addSubview:self];
            [UIView animateWithDuration:.2 animations:^{
                self.alpha = 1;
            }];
            
            break;
        }
    }
//    [swa show];
}
- (void)dismiss;
{
    [UIView animateWithDuration:.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
//    [swa dismiss];
}
@end




